package com.joshua.viewtest.demo.RecylerView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.joshua.viewtest.R;

/**
 * Created by LONG on 2016/10/18.
 */

public class DividerGridItemDecoration extends RecyclerView.ItemDecoration {
    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

    private Drawable mHDivider, mVDivider;
    private int dividerHeight;

    public DividerGridItemDecoration(Context context) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mVDivider = a.getDrawable(0);
        mHDivider = context.getResources().getDrawable(R.drawable.horizontal_divider_bg);
        a.recycle();
        dividerHeight = mHDivider.getIntrinsicHeight();
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        drawVertical(c, parent);
        drawHorizontal(c, parent);
    }

    //列数
    private int getSpanCount(RecyclerView parent) {
        int spanCnt = -1;
        LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            spanCnt = ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            spanCnt = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
        }
        return spanCnt;
    }

    public void drawVertical(Canvas c, RecyclerView parent) {
        final int childCount = parent.getChildCount();

        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getLeft() - params.leftMargin;
            final int right = child.getRight() + params.rightMargin
                    + dividerHeight;
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + dividerHeight;
            mVDivider.setBounds(left, top, right, bottom);
            mVDivider.draw(c);
        }
    }

    public void drawHorizontal(Canvas c, RecyclerView parent) {
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
                    child.getLayoutParams();
            final int top = child.getTop() - params.topMargin;
            final int bottom = child.getBottom() + params.bottomMargin
                    + dividerHeight;
            final int left = child.getRight() + params.rightMargin;
            final int right = left + dividerHeight;
            mHDivider.setBounds(left, top, right, bottom);
            mHDivider.draw(c);
        }
    }

    private boolean isLastColumn(RecyclerView parent, int pos, int spanCnt, int childCnt) {
        LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            if ((pos + 1) % 4 == 0)
                return true;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
            if (orientation == StaggeredGridLayoutManager.HORIZONTAL) {
                childCnt = childCnt - childCnt % spanCnt;
                if (pos >= childCnt)
                    return true;
            } else {
                if ((pos + 1) % spanCnt == 0)
                    return true;
            }
        }
        return false;
    }

    private boolean isLastRaw(RecyclerView parent, int pos, int spanCnt, int childCnt) {
        LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            childCnt = childCnt - childCnt % spanCnt;
            if (pos >= childCnt)
                return true;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                childCnt = childCnt - childCnt % spanCnt;
                if (pos >= childCnt)
                    return true;
            } else {
                if ((pos + 1) % spanCnt == 0)
                    return true;
            }
        }
        return false;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView
            .State state) {
        int spanCnt = getSpanCount(parent);
        int childCnt = parent.getAdapter().getItemCount();
        int position = parent.getChildAdapterPosition(view);
        if (isLastRaw(parent, position, spanCnt, childCnt)) {
            outRect.set(0, 0, dividerHeight, 0);
        } else if (isLastColumn(parent, position, spanCnt, childCnt)) {
            outRect.set(0, 0, 0, dividerHeight);
        } else {
            outRect.set(0, 0, dividerHeight, dividerHeight);
        }
    }


}
